package com.chaoli.reggie_diy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chaoli.reggie_diy.common.BaseContext;
import com.chaoli.reggie_diy.common.R;
import com.chaoli.reggie_diy.domain.AddressBook;
import com.chaoli.reggie_diy.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增收货地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> addAddressBook(@RequestBody AddressBook addressBook) {
        log.info("新增收货地址的信息={}", addressBook.toString());
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址的信息为={}", addressBook.toString());
        Long userId = BaseContext.getCurrentId();
        // 将以前的默认地址设置为0
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AddressBook::getIsDefault, 0).eq(AddressBook::getUserId, userId);
        // update address_book set is_default = 0 where user_id = ?
        addressBookService.update(updateWrapper);
        // 设置当前地址为默认地址
        addressBook.setIsDefault(1);
        // update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据id获取地址
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook == null) {
            return R.error("该地址不存在");
        }
        return R.success(addressBook);

    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .eq(AddressBook::getIsDefault, 1);
        // select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (addressBook == null) {
            return R.error("没有设置默认地址");
        }
        return R.success(addressBook);
    }

    /**
     * 查询指定用户的全部地址
     *
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> addressBookList(AddressBook addressBook) {
        // 给形参对象addressBook的UserId属性赋上当前登录用户的值
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("当前地址信息为：{}", addressBook.toString());
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        // 根据UserId查询用户的所有地址
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId())
                .orderByDesc(AddressBook::getUpdateTime);
        // select * from address_book where user_id = ? order by update_time desc
        List<AddressBook> addressBookList = addressBookService.list(queryWrapper);
        if (addressBookList == null){
            return R.error("当前用户还没有保存地址@@");
        }
        return R.success(addressBookList);

    }


}
